package com.wiloon.java.network.network.nettyhttpserver.nettyhttpclient;


import com.wiloon.java.network.network.nettyhttpserver.monitor.RequestMonitor;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by Roy on 2015/12/16;
 * thread pool
 * connection closed by client
 */
public class HttpPressTestThreadPool {
    private final static Logger logger = LoggerFactory.getLogger(HttpClient.class);

    public static void main(String[] args) {

        try {
            new Thread(new RequestMonitor()).start();
            EventLoopGroup workerGroup = new NioEventLoopGroup();
            HttpClient httpClient = new HttpClient(workerGroup);


            ConsoleParams.host = "127.0.0.1";
            ConsoleParams.port = 7777;
            ConsoleParams.urlSuffix = "/?msg=";
            ConsoleParams.sleepAfter = 1000;
            ConsoleParams.threadPoolSize =1;

            if (args.length > 0) {
                ConsoleParams.host = args[0];
                ConsoleParams.port = Integer.valueOf(args[1]);
                ConsoleParams.urlSuffix = args[2];
                ConsoleParams.sleepAfter = Integer.valueOf(args[3]);
                ConsoleParams.threadPoolSize = Integer.valueOf(args[4]);
            }
            logger.info("param:{}", ConsoleParams.print());
            ExecutorService executorService = Executors.newFixedThreadPool(ConsoleParams.threadPoolSize);
            int i = 1;


            while (true) {
                executorService.execute(new RequestExecutor(httpClient));
                if (i++ == ConsoleParams.sleepAfter) {
                    i = 1;
                    Thread.sleep(1000);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
